<?php
require_once 'Class/Drugs.php';
require_once 'Class/DiagnosisDrugs.php';
require_once 'Class/DiagnosisUserBase.php';
require_once 'Class/Diagnosis.php';
require_once 'Class/UserBase.php';
require_once 'Modules/Drugs.php';
require_once 'Modules/BaseModule.php';
require_once 'Modules/DiagnosisDrugs.php';
require_once 'Modules/DiagnosisUserBase.php';
require_once 'Modules/UserBase.php';

/**
 * 
 */
class M_Diagnosis extends ModuleBase{

	/**
	 *
	 * @param Diagnosis $Diagnosis
	 * @return Diagnosis
	 */
	public function insert($Diagnosis=null){
		if($Diagnosis===null){
			return null;
		}

		$o=new Diagnosis($Diagnosis);
		
		/**
		 *
		 * @todo Ini
		 */

		$o->Id=$this->db->guid();
		$o->DoctorId=SESSION::Certificate()->Id;
		$o->CreateDate=time();
		$o->ModifyDate=time();
		if(!isset($o->PatientId)|| empty($o->PatientId)){
			$o->PatientId=$this->db->guid();
		}
		
		if(isset($o->DiagnosisUserBase) && $o->DiagnosisUserBase!==null){
			$M_DiagnosisUserBase=new M_DiagnosisUserBase();
			$o->DiagnosisUserBase->Id=$this->db->guid();
			$o->DiagnosisUserBase->DiagnosisId=$o->Id;
			$M_DiagnosisUserBase->insert($o->DiagnosisUserBase);
						
			$M_UserBase=new M_UserBase();
			$UserBase=new UserBase($o->DiagnosisUserBase);
			$UserBase->Id=$o->PatientId;
			if($M_UserBase->single($UserBase)===null){
				$UserBase->CreateDate=time();
				$UserBase->ModifyDate=time();
				$UserBase->Status=1;
				$M_UserBase->insert($UserBase);
			}
		}
		if(isset($o->DiagnosisDrugs) && is_array($o->DiagnosisDrugs) && !empty($o->DiagnosisDrugs)){
			$M_Drugs=new M_Drugs();
			$M_DiagnosisDrugs=new M_DiagnosisDrugs();
			foreach($o->DiagnosisDrugs as $i){
				$i->Id=$this->db->guid();
				$i->DiagnosisId=$o->Id;
				$i->Status=1;
				$m_Drugs=$M_Drugs->single(json_decode('{"Id":"'.$i->DrugId.'"}'));
				if($m_Drugs===null){
					continue;
				}
				$i->Name=$m_Drugs->Name;
				$M_DiagnosisDrugs->insert($i);
			}
		}
		

		if($this->db->insert('Diagnosis',$o->__modetoarray())>0){
			$this->_dishsave('Diagnosis', $o->Id, $o);
		}
		else{

			$o->Id=null;

		}
		return $o;
	}

	/**
	 *
	 * @param Diagnosis $Diagnosis
	 * @return int
	 */
    public function update($Diagnosis=null){
      if($Diagnosis===null){
          return null;
      }

      $o=new Diagnosis($Diagnosis);
      $o->DoctorId=SESSION::Certificate()->Id;
      $o->ModifyDate=time(); 
      /**
      *
      * @todo Condition
      */

      if($this->db->update('Diagnosis',$o->__modetoarray(array('Reseaon'=>$o->Reseaon,'Property_Ask'=>$o->Property_Ask,'Property_Look'=>$o->Property_Look,'Property_Listen'=>$o->Property_Listen,'Property_Touch'=>$o->Property_Touch,'Property_Result'=>$o->Property_Result,'Massage'=>$o->Massage,'Remarks'=>$o->Remarks,'CreateDate'=>$o->CreateDate,'ModifyDate'=>$o->ModifyDate,'Status'=>$o->Status)),array('Id'=>$o->Id,'DoctorId'=>$o->DoctorId,'PatientId'=>$o->PatientId))>0){
      	if(isset($o->DiagnosisUserBase) && !empty($o->DiagnosisUserBase)){
      		$M_DiagnosisUserBase=new M_DiagnosisUserBase();
      		$o->DiagnosisUserBase->Id=$this->db->guid();
      		$o->DiagnosisUserBase->DiagnosisId=$o->Id;
      		$M_DiagnosisUserBase->update($o->DiagnosisUserBase);
      	}
      	if(isset($o->DiagnosisDrugs) && is_array($o->DiagnosisDrugs) && !empty($o->DiagnosisDrugs)){
      		$M_Drugs=new M_Drugs();
      		$M_DiagnosisDrugs=new M_DiagnosisDrugs();
      		$M_DiagnosisDrugs->linkdelete($o);
      		foreach($o->DiagnosisDrugs as $i){
      			$i->Id=$this->db->guid();
      			$i->DiagnosisId=$o->Id;
      			$i->Status=1;
      			$m_Drugs=$M_Drugs->single(json_decode('{"Id":"'.$i->DrugId.'"}'));
      			if($m_Drugs===null){
      				continue;
      			}
      			$i->Name=$m_Drugs->Name;
      			$M_DiagnosisDrugs->insert($i);
      		}
      	}
      	
      	$this->_dishsave('Diagnosis', $o->Id, $o);
        return 1;
      }else{
        return 0;
      }
    }
	

	/**
	 * @param Diagnosis $Diagnosis
	 * return int
	 */
	public function delete($Diagnosis=null){
		if($Diagnosis===null||!isset($Diagnosis->Id)||empty($Diagnosis->Id)){
			return null;
		}
		return $this->db->update('Diagnosis',array('Status'=>-1),array('Id'=>$Diagnosis->Id));
	}
	
	/**
	 * @param Diagnosis $Diagnosis
	 */
	public function single($Diagnosis=null){
		if($Diagnosis===null||!isset($Diagnosis->Id)||empty($Diagnosis->Id)){
			return null;
		}
		$o=$this->_dishread('Diagnosis',$Diagnosis->Id);
	
		if($o===null){
			foreach($this->db->select('Diagnosis',array('Id'=>$Diagnosis->Id)) as $i){
				$o=new Diagnosis($i);
				if($o!==null){
					foreach($this->db->select('DiagnosisUserBase',array('DiagnosisId'=>$o->Id)) as $j){
						$o->DiagnosisUserBase=new DiagnosisUserBase($j);break;
					}
					foreach($this->db->select('DiagnosisDrugs',array('DiagnosisId'=>$o->Id,'Status'=>1)) as $j){
						if(!isset($o->DiagnosisDrugs)||empty($o->DiagnosisDrugs)){
							$o->DiagnosisDrugs=array();
						}
						$o->DiagnosisDrugs[]=new DiagnosisDrugs($j);
					}
					$this->_dishsave('Diagnosis',$o->Id,$o);
				}
			}
		}
		return $o;
	}
	
	/**
	 *
	 * @param object {'PageIndex':0,'PageSize':20}
	 */
	public function arraylist($search=null){
		$li=array();
		$condkey=array();$condval=array();
		
		$condkey[]='[DoctorId]=?';
		$condval[]=SESSION::Certificate()->Id;
		
		$PageIndex=0;$PageSize=20;
		if($search!==null){
			if(isset($search->PatientId)){
				$condkey[]='[PatientId]=?';
				$condval[]=$search->PatientId;
			}
			if(isset($search->PatientName)){
				$condkey[]='[Name]=?';
				$condval[]=$search->PatientName;
			}
			if(isset($search->PatientEmail)){
				$condkey[]='[Email]=?';
				$condval[]=$search->PatientEmail;
			}
			if(isset($search->PatientMobile)){
				$condkey[]='[Mobile]=?';
				$condval[]=$search->PatientMobile;
			}
			if(isset($search->MinCreate)){
				$condkey[]='[CreateDate]>=?';
				$condval[]=intval($search->MinCreate);
			}
			if(isset($search->MaxCreate)){
				$condkey[]='[CreateDate]<=?';
				$condval[]=intval($search->MaxCreate);
			}
			if(isset($search->Status)){
				$condkey[]='[Status]=?';
				$condval[]=intval($search->Status);
			}
			
			if(is_numeric($search->PageIndex)){
				$PageIndex=$search->PageIndex;
			}
			if(is_numeric($search->PageSize)){
				$PageSize=$search->PageSize;
			}
		}
		
		$pdo=$this->db->pdo();
		$sth = $pdo->prepare('select [PatientId],[CreateDate],[Status],DiagnosisUserBase.* from Diagnosis inner join DiagnosisUserBase on Diagnosis.Id=DiagnosisUserBase.DiagnosisId where '.implode(' and ', $condkey));

		if($sth->execute($condval)){
			$M_DiagnosisDrugs=new M_DiagnosisDrugs();
			foreach($sth->fetchAll() as $i){
				$o=new Diagnosis();
				$o->Id=$i['DiagnosisId'];
				$o->PatientId=$i['PatientId'];
				$o->CreateDate=intval($i['CreateDate']);
				$o->Status=intval($i['Status']);
				$o->DiagnosisUserBase=new DiagnosisUserBase($i);
				$o->DiagnosisDrugs=$M_DiagnosisDrugs->childrens($o);
				$li[]=$o;
			}
		}
		return $li;
	}

}


?>